package Projo;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import CommonPanel.HeaderPanel;
import Exception.CheckDataException;
import Exception.InsertionDataException;
import Exception.LoadDriverException;
import Exception.OpenConnectionException;
import General.TreatError;
import GenericMethod.SetDimension;
import Teacher.WindowTeacher;

public class ProjoScreen extends JFrame
{
	private PanelShow panelShow;
	private ThreadBar threadBar;
	private Container container;
	private HeaderPanel headerPanel;
	private ArrayList<Student> listStudent;
	private SetDimension setDimension;
	private Connection connection;
	
	public ProjoScreen(ArrayList<Student> arrayStudent)
	{
		super("Projo");
				
		setDimension = new SetDimension();
		setDimension.setBoundFrame(this, 800, 1000);
		
		listStudent = new ArrayList<Student>(arrayStudent);
				
		MyWindowListener windowListener = new MyWindowListener();
		this.addWindowListener(windowListener);
		
		headerPanel = new HeaderPanel(); //Create the headerPanel
		panelShow = new PanelShow(listStudent, connection);

		container = new Container();
		this.add(container);
		container = getContentPane();
		
		container.setLayout(new BorderLayout());
		container.add(headerPanel, BorderLayout.NORTH);
		container.add(panelShow, BorderLayout.CENTER);
		
		setResizable(false); //Not allow to resize
		setVisible(true);
	}
	
	private class MyWindowListener extends WindowAdapter
	{
		public void windowClosing(WindowEvent arg0)
		{
			try
			{
				if(JOptionPane.showConfirmDialog(null, "Are you sure to close this frame ?", "Close projector frame", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
				{
					for(Student student : listStudent)
						student.getThread().end();
					dispose();
					new WindowTeacher();
				}
			}
			catch(LoadDriverException | CheckDataException | OpenConnectionException | InsertionDataException error)
			{
				TreatError.showError(error);
			}
		}
	}
}
